package de.herberlin.webapp.db;

import java.util.logging.LogManager;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.herberlin.webapp.core.Configuration;
import de.herberlin.webapp.core.Defaultdata;

public class InitListener implements ServletContextListener{
	
	private Log logger=LogFactory.getLog(getClass());

	public void contextDestroyed(ServletContextEvent arg0) {
		logger.info("Context destroyed..");
		
	}

	public void contextInitialized(ServletContextEvent ev) {
		try {
			logger.info("Reading jdk-logging configuration..");
			LogManager.getLogManager().readConfiguration(getClass().getResourceAsStream("/logging.properties"));
			logger=LogFactory.getLog(getClass());
		} catch (Exception e) {
			logger.error(e,e);
			e.printStackTrace();
		}
		try {
			String className=Configuration.getInstance().getSetting(getClass(),"defaultdata");
			if (className==null) {
				logger.warn("Defaultdata factory is null!");
			} else {
				logger.info("Initializing defaultdata from "+className);
				Defaultdata defaultdata=(Defaultdata)Class.forName(className).newInstance();
				defaultdata.init(ev.getServletContext());
			}
		} catch (Exception e) {
			logger.error(e,e);
			e.printStackTrace();
		}
		logger.info("Context created..");
		
	}

}
